<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    center>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="留言内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="留言内容"></el-input>
    </el-form-item>
    <el-form-item label="读者id" prop="readerId">
      <el-input v-model="dataForm.readerId" placeholder="读者id"></el-input>
    </el-form-item>
    <el-form-item label="文章id" prop="articleId">
      <el-input v-model="dataForm.articleId" placeholder="文章id"></el-input>
    </el-form-item>
    <el-form-item label="点赞数" prop="likeCount">
      <el-input v-model="dataForm.likeCount" placeholder="点赞数"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          content: '',
          readerId: '',
          articleId: '',
          likeCount: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          content: [
            { required: true, message: '留言内容不能为空', trigger: 'blur' }
          ],
          readerId: [
            { required: true, message: '读者id不能为空', trigger: 'blur' }
          ],
          articleId: [
            { required: true, message: '文章id不能为空', trigger: 'blur' }
          ],
          likeCount: [
            { required: true, message: '点赞数不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/admin/articlenote/info/${this.dataForm.id}`,
              method: 'get'
            }).then((response) => {
              if (response && response.respCode == "200") {
                this.dataForm.content = response.data.content
                this.dataForm.readerId = response.data.readerId
                this.dataForm.articleId = response.data.articleId
                this.dataForm.likeCount = response.data.likeCount
                this.dataForm.createTime = response.data.createTime
                this.dataForm.updateTime = response.data.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: `/admin/articlenote`,
              method: this.dataForm.id == 0 ? 'post' : 'put',
              data: {
                'id': this.dataForm.id || undefined,
                'content': this.dataForm.content,
                'readerId': this.dataForm.readerId,
                'articleId': this.dataForm.articleId,
                'likeCount': this.dataForm.likeCount,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              }
            }).then((response) => {
              if (response && response.respCode == "200") {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(response.respMessage)
              }
            })
          }
        })
      }
    }
  }
</script>
